#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
string all[50];
int path[50];
int dp[50];
int _in[50];
int main() {
    int n,m;
    cin>>n>>m;
    for (int i=0; i<n; ++i) cin>>all[i];
    memset(path, -1, sizeof(path));
    memset(dp, 0, sizeof(dp));
    memset(_in, 0, sizeof(_in));
    for (int i=1; i<n; ++i) {
        int max=0;
        for (int j=0; j<i; ++j) {
            if (all[j]<all[i] && dp[j]>dp[max]) max=j;
        }
        dp[i]=dp[max]+1;
        path[i]=max;
    }
    int max=0;
    for (int i=0; i<n; ++i) if (dp[i]>dp[max]) max=i;
    int st=max;
    int flag;
    while (path[st]!=-1) {
        _in[st]=true;
        flag=st;
        st=path[st];
    }
    cout<<"flag: "<<flag<<endl;
    string res[50];
    for (int i=0; i<flag; ++i) res[i]=all[flag];
    for (int i=flag; i<n; ++i) {
        if (_in[i]==true) {
            flag=i;
            res[i]=all[flag];
        }
        else {
            res[i]=all[flag];
        }
    }
    for (int i=0; i<n; ++i) cout<<res[i]<<endl;
    return 0;
}
